//
//  GWUserDefaultsObj.m
//  ShandsMod
//
//  Created by zh_s on 2019/9/27.
//  Copyright © 2019 zh_s. All rights reserved.
//

#import "GWUserDefaultsObj.h"

@implementation GWUserDefaultsObj
#pragma mark - 创建单例
//本类单例
+ (instancetype)sharedManager {
    NSString *className = [NSString stringWithFormat:@"%@", [self class] ];
    Class class = NSClassFromString( className );
    static dispatch_once_t once;
    static id singleton;
    dispatch_once(&once, ^ {
        singleton = [[class alloc] init];
    });
    return singleton;
}
#pragma mark - code  encode
- (void)copyWithOtherObject:(GWUserDefaultsObj *)pObject
{
    unsigned int propertyCount = 0;
    objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);
    
    for ( int i = 0; i < propertyCount; i++ ) {
        objc_property_t *thisProperty = propertyList + i;
        const char* propertyName = property_getName(*thisProperty);
        NSString *strPropertyName = [NSString stringWithUTF8String:propertyName];
        NSString *strFirst = [[strPropertyName substringToIndex:1] capitalizedString ];
        strPropertyName = [strPropertyName substringFromIndex:1];
        NSString *objName = [NSString stringWithFormat:@"%@%@", strFirst, strPropertyName];
        
        NSString *setMethodName = [NSString stringWithFormat:@"set%@:", objName ];
        NSString *getMethodName = [NSString stringWithUTF8String:propertyName];
        SuppressPerformSelectorLeakWarning([self performSelector:NSSelectorFromString(setMethodName) withObject: [pObject performSelector:NSSelectorFromString(getMethodName)]]);
    }
    free(propertyList);
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    unsigned int propertyCount = 0;
    objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);
    
    for ( int i = 0; i < propertyCount; i++ ) {
        objc_property_t *thisProperty = propertyList + i;
        const char* propertyName = property_getName(*thisProperty);
        NSString *methodName = [NSString stringWithUTF8String:propertyName];
        SuppressPerformSelectorLeakWarning([aCoder encodeObject:[self performSelector:NSSelectorFromString(methodName)] forKey:methodName]);
    }
    free(propertyList);
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    if ( self = [super init]) {
        unsigned int propertyCount = 0;
        objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);
        
        for ( int i = 0; i < propertyCount; i++ ) {
            objc_property_t *thisProperty = propertyList + i;
            const char* propertyName = property_getName(*thisProperty);
            NSString *strPropertyName = [NSString stringWithUTF8String:propertyName];
            NSString *strFirst = [[strPropertyName substringToIndex:1] capitalizedString ];
            strPropertyName = [strPropertyName substringFromIndex:1];
            NSString *objName = [NSString stringWithFormat:@"%@%@", strFirst, strPropertyName];
            
            NSString *setMethodName = [NSString stringWithFormat:@"set%@:", objName ];
            NSString *getMethodName = [NSString stringWithUTF8String:propertyName];
            id obj = [aDecoder decodeObjectForKey:getMethodName];
            if (obj ==nil) {
                obj = @"";
            }
            
            SuppressPerformSelectorLeakWarning([self performSelector:NSSelectorFromString(setMethodName) withObject: obj]);
        }
        free(propertyList);
    }
    return self;
}

@end
